package org.sensolab.metacrumbs;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sensolab.metacrumbs.data.CrumbsManager;
import org.sensolab.metacrumbs.data.PMF;
import org.sensolab.metacrumbs.data.model.MetaLocation;

import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class MetacrumbsServlet extends HttpServlet {
	
	private void createTestLocations(PersistenceManager pm, User user) {
		int count = 5;
		GeoPt geoPoint = new GeoPt(45.51956f, 9.36494f);
		MetaLocation loc;
		for (int i=1; i<=count; i++) {
			loc = CrumbsManager.newUserLocation(pm, user, geoPoint, "Test Location #" + i);
			System.out.println("Location '" + loc.getTitle() + "' created");
		}
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		UserService userService = UserServiceFactory.getUserService();
        String thisURL = req.getRequestURI();
        resp.setContentType("text/html");
        if (req.getUserPrincipal() != null) {
            
        	// USER LOGGED IN
        	User loggedUser = userService.getCurrentUser();
           	resp.getWriter().println(
            		"<p>Hello, " + loggedUser.getUserId() + "!<br>" +
            		"You can <a href=\"" + userService.createLogoutURL(thisURL) + "\">sign out</a>.</p>"
            		);
        	
        	PersistenceManager pm = PMF.get().getPersistenceManager();

        	try {
	        	List<MetaLocation> userLocations = CrumbsManager.getUserLocations(pm, loggedUser);
	        	
	        	if (userLocations==null || userLocations.size()==0) {
	        		createTestLocations(pm, loggedUser);
	        	}
	        	
	        	userLocations = CrumbsManager.getUserLocations(pm, loggedUser);
	        	
	        	StringBuffer sb = new StringBuffer();
	        	sb.append("<table cellpadding='3' cellspacing='0' border='1'>");
	        	sb.append("<thead>");
	        	sb.append("<tr>");
	        	sb.append("<th>Title</th>");
	        	sb.append("<th>Geo Point</th>");
	        	sb.append("<th>Date Created</th>");
	        	sb.append("<th>Date Last modified</th>");
	        	sb.append("<th>Description</th>");
	        	sb.append("</tr>");
	        	sb.append("</thead>");
	        	
	        	sb.append("<tbody>");
	        	for (MetaLocation location : userLocations) {
	        		sb.append("<tr>");
	            	sb.append("<td>" + location.getTitle() + "</td>");
	            	sb.append("<td>" + location.getGeoPoint() + "</td>");
	            	sb.append("<td>" + location.getCreationDate() + "</td>");
	            	sb.append("<td>" + location.getLastUpdated() + "</td>");
	            	sb.append("<td>" + location.getDescription() + "</td>");
	        		sb.append("</tr>");
	        	}
	        	sb.append("</tbody>");
	        	
	        	sb.append("</table>");
	        	
	        	resp.getWriter().println(sb.toString());
	        	
        	} catch (Exception e) {
        		// TODO fix logging
        		e.printStackTrace();
        	} finally {
        		pm.close();
        	}
    		
        } else {
        	
        	// USER NOT LOGGED IN
        	
            resp.getWriter().println(
            		"<p>Please <a href=\"" + userService.createLoginURL(thisURL) + "\">sign in with your Google Account</a>.</p>"
            		);
            
        }        
        
		
	}
	
}
